package com.wl156.utils;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

public class AddressStringUtil {
	public static double[] getLocationInfo(String address) {
		HttpClient hc = new DefaultHttpClient();
		HttpGet get = new HttpGet(
				"http://maps.google.com/maps/api/geocode/json?address="
						+ address + "&sensor=false");
		try {
			HttpResponse resp = hc.execute(get);
			System.out.println(resp.getStatusLine());
			JSONObject json = new JSONObject(EntityUtils.toString(resp
					.getEntity()));
			JSONObject location = json.getJSONArray("results").getJSONObject(0)
					.getJSONObject("geometry").getJSONObject("location");
			double lng = location.getDouble("lng");
			double lat = location.getDouble("lat");
			System.out.println("lng=" + lng + ", lat" + lat);
			return new double[] { lat, lng };
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
